package org.e_gold.factory;

import org.e_gold.service.BrowserService;
import org.e_gold.util.CalcUtil;
import org.e_gold.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class Manager extends Thread {

    public static Integer STATUS = 0; // init apps - not select
    @Autowired
    private BrowserService browserService;

    @Override
    public void run() {

        try {
            LogUtil.logStart();
            while (true) {
                if (STATUS == 0) {
                    browserService.exposeEvents();
                    // waitingMinutes(1, 2);
                    waitingSeconds(5, 15);
                }
                if (STATUS == 1) {
                    browserService.waitingResult();
                    // waitingMinutes(1, 4);
                    waitingSeconds(39, 79);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // waiting random minute from - to , ex : 5-10 minutes
    // production
    @SuppressWarnings("unused")
    private void waitingMinutes(int from, int to) throws InterruptedException {

        Float waitingRandomSeconds = (float) CalcUtil.randomTime(from * 60, to * 60);
        Float waitingNextTime = waitingRandomSeconds / 60;
        LogUtil.logWaitingNextTime(waitingNextTime, " minutes");
        Thread.sleep((int) (waitingRandomSeconds * 1000));
    }

    // development
    private void waitingSeconds(int from, int to) throws InterruptedException {

        Float waitingRandomSeconds = (float) CalcUtil.randomTime(from, to);
        Float waitingNextTime = waitingRandomSeconds;
        LogUtil.logWaitingNextTime(waitingNextTime, "seconds");
        Thread.sleep((int) (waitingRandomSeconds * 1000));
    }

}
